import { useReducer } from 'react';

/**
 * 仓库标识
 * */
export interface IStateType {
  projectInfo: Record<string, any>;
  SourceInfo: Record<string, any>;
  Source_Invite: Record<string, any>;
  Invite_PackList: Record<string, any>[];
  Calibrate_Id: string;
  Source_Clarify: Record<string, any>;
  Source_PurchaseChange: Record<string, any>;
  Source_Calibrate: Record<string, any>;
  Source_CurrentStep: number;
}

/**
 * 状态枚举
 * */
export enum IUpdateEnum {
  UPDATE_PROKECT_INFO = 'update',
  Set_SourceInfo = 'Set_SourceInfo',
  Set_Source_Invite = 'Set_Source_Invite',
  Set_Invite_PackList = 'Set_Invite_PackList',
  Set_Calibrate_Id = 'Set_Calibrate_Id',
  Set_Source_Clarify = 'Set_Source_Clarify',
  Set_Source_PurchaseChange = 'Set_Source_PurchaseChange',
  Set_Source_Calibrate = 'Set_Source_Calibrate',
  Set_Source_CurrentStep = 'Set_Source_CurrentStep',
}

/**
 * action枚举
 * */
export interface IActionEnum {
  type: IUpdateEnum;
  payload?: any;
}

/**
 * 状态仓库
 * */
export const useUpdateReducer = () => {
  /**
   * 初始化状态
   * */
  const initState: IStateType = {
    projectInfo: {}, // 项目对象
    SourceInfo: {}, // 实施详情
    Source_Invite: {}, // 招标公告详情
    Invite_PackList: [], // 招标公告选中公开的数据
    Calibrate_Id: '', //定标id
    Source_Clarify: {}, //澄清详情
    Source_PurchaseChange: {}, //【好像暂时没用到】
    Source_Calibrate: {}, // 定标详情
    Source_CurrentStep: 0, //步骤条
  };

  /**
   * 派发处理
   * */
  const updateReducer = (state: IStateType, action: IActionEnum) => {
    switch (action?.type) {
      case IUpdateEnum.UPDATE_PROKECT_INFO:
        return {
          ...state,
          projectInfo: action?.payload,
        };
      case IUpdateEnum.Set_SourceInfo:
        return {
          ...state,
          SourceInfo: action?.payload,
        };
      case IUpdateEnum.Set_Source_Invite:
        return {
          ...state,
          Source_Invite: action?.payload,
        };
      case IUpdateEnum.Set_Invite_PackList:
        return {
          ...state,
          Invite_PackList: action?.payload,
        };
      case IUpdateEnum.Set_Calibrate_Id:
        return {
          ...state,
          Calibrate_Id: action?.payload,
        };
      case IUpdateEnum.Set_Source_Clarify:
        return {
          ...state,
          Source_Clarify: action?.payload,
        };
      case IUpdateEnum.Set_Source_PurchaseChange:
        return {
          ...state,
          Source_PurchaseChange: action?.payload,
        };
      case IUpdateEnum.Set_Source_Calibrate:
        return {
          ...state,
          Source_Calibrate: action?.payload,
        };
      case IUpdateEnum.Set_Source_CurrentStep:
        return {
          ...state,
          Source_CurrentStep: action?.payload,
        };
      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, dispatch] = useReducer(updateReducer, initState);

  return {
    ...state,
    dispatch,
  };
};
